package com.zzld.modules.maintenance.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.maintenance.entity.Path;
import com.zzld.modules.maintenance.service.IPathService;

@Controller
@RequestMapping("/maintenance/path")
public class MtanPathController {
	
	@Autowired
	private IPathService pathService;
	
	private String listurl = "/modules/maintenance/path/pathList";
	private String licru = "/modules/maintenance/path/pathCru";
	
	
	@RequestMapping(method=RequestMethod.GET)
	public String list(){
		return listurl;
	}
	
	@RequestMapping("/Cru")
	public String li(){
		return licru;
	}
	
	@RequestMapping(value="/del/{id}",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON delbyid(HttpServletRequest request,
			@RequestParam String id){
		CommonJSON json = new CommonJSON();
		try{
			if(StringUtils.isNotBlank(id)){
				if(pathService.deleteByPrimaryKey(id)>0){
					json.setMessage(MessageUtils.DELETE_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.DELETE_WARNING);
					json.setSuccess(false);
				}
			}
			
		}catch(Exception e){
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping(value="/update/pro",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(HttpServletRequest request,Path entity){
		CommonJSON json = new CommonJSON();
		try{
			if(StringUtils.isBlank(entity.getId())){
				//新增
				entity.setId(UuidUtil.uuid().toString());
				entity.setCreateTime(new Date());
				if(pathService.saveSelective(entity)>0){
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			}else{
				//修改
				if(pathService.updateByPrimaryKeySelective(entity)>0){
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("/ser/{id}")
	@ResponseBody
	public JSONObject map(HttpServletRequest request,@RequestParam String id){
		JSONObject json = new JSONObject();
		 try{
			 if(StringUtils.isNotBlank(id)){
				 Path entity = pathService.selectByPrimaryKey(id);
				 json.put("id", entity.getId());
				 json.put("pathname", entity.getPathname());
				 json.put("pid", entity.getPid());
				 json.put("islat", entity.getIslat());
				 json.put("sort", entity.getSort());
			 }
		 }catch(Exception e){
			 e.printStackTrace();
		 }
		return json;
	}
	
	
	@RequestMapping("/treegrid")
	@ResponseBody
	public List<TreeGrid> treegrid(){
		return pathService.treeGrid();
	}
}
